//
//  LoginYunPlaVC.m
//  reSearchDemo
//
//  Created by 云库 on 15/7/24.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "LoginYunPlaVC.h"
#import "MenuView.h"
#import "OnePlatform.h"
#import "BackPassWord.h"
#import "HTTPGlobals.h"
@interface LoginYunPlaVC (){
    UIView                 * actionBar;
    NSMutableArray         * _textinfoArr;
    UITextField            *_text;
}
@property(nonatomic,retain)MenuView * menuView;
@end

@implementation LoginYunPlaVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _textinfoArr=[[NSMutableArray alloc]initWithCapacity:2];
    // Do any additional setup after loading the view.
    self.title = @"登录云平台";
    self.view.backgroundColor=[UIColor whiteColor];
    [self setRightBarButtonImage:LOADIMAGE(@"btn_more") highlightedImage:nil selector:@selector(menu)];
    
//    UIView * actionbar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 44)];
//    self.navigationItem.titleView = actionbar;
//    UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(8, 0, 100, 40)];
//    lab.text=@"登录云平台";
//    lab.font=[UIFont systemFontOfSize:20];
//    lab.textColor=[UIColor whiteColor];
//    [actionbar addSubview:lab];
    [self buildMainView];
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
    back.title = @"";
    self.navigationItem.backBarButtonItem = back;
    
}
-(void)buildMainView{
    for (int i=0; i<2; i++) {
        UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(30, 60+64+i*50, 60, 40)];
        lab.backgroundColor=[UIColor groupTableViewBackgroundColor];
        lab.textColor=[UIColor blackColor];
        lab.text=[NSString stringWithFormat:@"%@",i?@"密码:":@"账号:"];
        lab.textAlignment=NSTextAlignmentCenter;
        [self.view addSubview:lab];
    }
    for (int i=0; i<2; i++) {
        _text=[[UITextField alloc]initWithFrame:CGRectMake(30+60, 60+64+i*50, CGRectGetWidth(self.view.frame) - 30*2 - 60, 40)];
        _text.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _text.placeholder=[NSString stringWithFormat:@"%@",i?@"":@"请输入你的邮箱或平台号登录"];
        _text.tag=10+i;
        _text.borderStyle=UITextBorderStyleNone;
        _text.font=[UIFont systemFontOfSize:14];
        [self.view addSubview:_text];
        [_textinfoArr addObject:_text.text];
        if (i == 1) {
            _text.secureTextEntry = YES;
        }
    }
    UIButton * login=[UIButton buttonWithType:UIButtonTypeSystem];
    login.frame=CGRectMake(20, 60+64+50+40+30, self.view.frame.size.width-40, 35);
    login.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [login setTitle:@"登录" forState:UIControlStateNormal];
    [login addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:login];
    
    UIButton * forgetPassword=[UIButton buttonWithType:UIButtonTypeSystem];
    forgetPassword.frame=CGRectMake(self.view.frame.size.width/2-80, self.view.frame.size.height-70-40, 160, 40);
    [forgetPassword setTitle:@"忘记密码？点这找回" forState:UIControlStateNormal];
    [forgetPassword addTarget:self action:@selector(forgetPassword:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetPassword];
}
//登录按钮
-(void)loginBtnClick:(UIButton*)sender{
    /*
    NSString *name;
    NSString *password;
    if (_text.tag==10) {
        name = _text.text;
        if (name.length<=0) {
            UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"注意" message:@"账号不可以为空" delegate:nil cancelButtonTitle:@"退出" otherButtonTitles:nil, nil];
            [alert show];
            return;
        }
    }else if(_text.tag==11){
        password = _text.text;
        if (password.length<=0) {
            UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"注意" message:@"密码不可以为空" delegate:nil cancelButtonTitle:@"退出" otherButtonTitles:nil, nil];
            [alert show];
            return;
        }
    }
     */
    //登录请求和判断
    /*
    NSDictionary *parme = [NSDictionary dictionaryWithObjectsAndKeys:name,@"username",password,@"password", nil];
    NSString *url = @"MobilePort/Index/login.html";
    [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:url parameters:parme finish:^(BOOL seccess, NSString *error, id obj) {
        DLog(@"obj = %@",obj);
        DLog(@"obj.info = %@",obj[@"info"]);
        if (seccess) {
            if ([obj[@"status"] intValue] > 0) {
                OnePlatform * platform=[[OnePlatform alloc]init];
                [self pushViewController:platform];
            }else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"登录失败,账户或密码输入有误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                return;
            }
            
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"登录失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            return;
        }
    }];
    */
    
    [self.HUD show:YES];
    //登录请求
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"15777777777",@"username",@"123456",@"password", nil];
    
    HTTPGlobals *status = [HTTPGlobals shareHTTPGlobal];
//    status.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//    status.manager.requestSerializer = [ AFHTTPRequestSerializer serializer ];
//    status.manager.responseSerializer = [ AFHTTPResponseSerializer serializer ];
    [status PostHTTPRequest:@"MobilePort/Index/login.html" parameters:dic finish:^(BOOL seccess, NSString *error, id obj) {
        [self.HUD hide:YES];
        if (seccess) {
            DLog(@"obj = %@",obj);
            DLog(@"obj.info = %@",obj[@"info"]);
            
            
        }else {
            
            UIAlertView *alert = [self showAlertViewTitle:@"提示:" message:@"登录失败，请检查网络" Btntitle:@"确定"];
            [alert show];
        }
        
    }];
    OnePlatform * platform=[[OnePlatform alloc]init];
    [self pushViewController:platform];
}
//忘记密码按钮
-(void)forgetPassword:(UIButton*)sender{
    [self pushViewController:[BackPassWord new]];
}
-(void)menu{
    /*
    NSArray * arr = nil;
    arr = @[@"登陆云平台", @"注册云平台"];
    _menuView = [[MenuView alloc] initWithButtonTitles:arr withDelegate:self];
    _menuView.hasImage = NO;
    [_menuView showInView:self.view origin:CGPointMake(self.view.width - 180, 70)];
    _menuView.buttonView.delegate=self;
     */
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_menuView hide];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
